package com.dwxt.community.controller;

import com.dwxt.common.annotation.*;
import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.entity.*;
import com.dwxt.community.entity.vo.*;
import com.dwxt.community.service.*;
import com.github.binarywang.utils.qrcode.*;
import org.apache.commons.lang.*;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.*;
import java.io.*;
import java.text.ParseException;
import java.util.*;

@RequestMapping("/cms/user")
@RestController
public class UserController {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(IDUtils.genItemId());
        }
    }

    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;

    /**
     * showdoc
     *
     * @param phone    必选 string 电话
     * @param password 必选 string 密码
     * @param code     必选 string 验证码
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 用户注册
     * @description 用户注册
     * @url /cms/user/register
     */
    @RequestMapping("/register")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "password", notNull = true),
            @ValidateFiled(index = 0, filedName = "code", notNull = true),
    })
    public BaseResult register(UserVO userVO) {
        return userService.create(userVO);

    }

    /**
     * showdoc
     *
     * @param phone    必选 string 电话
     * @param password 必选 string 密码
     * @param code     必选 string 验证码
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 忘记密码
     * @description 忘记密码，重置密码
     * @url /cms/user/resetPassword
     */
    @RequestMapping("/resetPassword")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "password", notNull = true),
            @ValidateFiled(index = 0, filedName = "code", notNull = true),
    })
    public BaseResult resetPassword(UserVO userVO) {
        return userService.resetPassword(userVO);
    }

    /**
     * showdoc
     *
     * @param phone 必选 string 电话
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 用户注册发送验证码
     * @description 用户注册发送验证码
     * @url /cms/user/sendVerificationCode
     */
    @RequestMapping("/sendVerificationCode")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
    })
    public BaseResult sendVerification(String phone) {
        return smsService.sendCaptcha(phone);
    }

    /**
     * showdoc
     *
     * @param phone    必选 string 电话
     * @param password 必选 string 密码
     * @return {"status":200,"msg":"SUCCESS","data":{"user":{"id":"158502014539731","phone":"15123058497","username":"yanlong","integral":100,"isNameAuthentication":0,"identityCard":"513030199201088419","isHouseHead":1,"headPhotoUrl":"http://127.0.0.1:8052/upload/158502014539731.png","lastSignTime":1585031602000,"createTime":1585048945000,"communityId":1,"todayIntegralMax":100,"communityName":"聚金万佳苑","houseNumber":"10栋32-6"},"token":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxNTg2OTQwNzMwNzU4NTkiLCJpYXQiOjE1ODY5NDA3MzEsInN1YiI6IjE1ODUwMjAxNDUzOTczMSIsImV4cCI6MTU4Njk0NDMzMX0.lTZ8jIYy-Hec3GJCXFs25zrtmPDPE_IZTQqR8NMz9J4"}}
     * @catalog 社区/用户
     * @title 登录
     * @description 登录
     * @url /cms/user/login
     */
    @RequestMapping("/login")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "password", notNull = true)
    })
    public BaseResult login(User user) {
        user = userService.login(user.getPhone(), user.getPassword());
        if (user != null) {
            String dongMeiId = user.getDongMeiId();
            String token = JwtUtils.createJWT(user.getId() + "," + dongMeiId, (long) 7 * 24 * 60 * 60 * 1000);
            //redisService.set(user.getId(), JsonUtils.objectToJson(user), (long) 30 * 60);
            //redisService.set(token, MD5Utils.MD5Encode(token), (long) 10 * 60);
            Map map = new HashMap();
            map.put("token", token);
            map.put("user", user);
            return BaseResult.ok(map);
        } else {
            return BaseResult.build(StatusCode.USERNAME_PASSWORD_WRONG);
        }

    }

    /**
     * showdoc
     *
     * @param oldPassword 必选 string 旧密码
     * @param newPassword 必选 string 新密码
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 修改密码
     * @description 修改密码
     * @url /cms/user/updatePassword
     */
    @RequestMapping("/updatePassword")
    public BaseResult updatePassword(String oldPassword, String newPassword) {
        int i = userService.updatePassword(oldPassword, newPassword);
        return i > 0 ? BaseResult.ok() : BaseResult.build(StatusCode.USER_UPDATE_FAIL);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158502014539731","phone":"15123058497","username":"yanlong","integral":100,"isNameAuthentication":0,"identityCard":"513030199201088419","isHouseHead":1,"headPhotoUrl":"http://127.0.0.1:8052/upload/158502014539731.png","lastSignTime":1585031602000,"createTime":1585048945000,"communityId":1,"todayIntegralMax":100,"communityName":"聚金万佳苑","houseNumber":"10栋32-6"}}
     * @catalog 社区/用户
     * @title 签到
     * @description 签到得积分
     * @url /cms/user/sign
     */
    @RequestMapping("/sign")
    public BaseResult sign() {
        return userService.sign();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":[{"id":2,"year":2020,"month":4,"day":15,"userId":"158502014539731","createTime":1586941365000}]}
     * @catalog 社区/用户
     * @title 查询本月的签到记录
     * @description 查询本月的签到记录
     * @url /cms/user/selectSignRecordList
     */
    @RequestMapping("/selectSignRecordList")
    public BaseResult selectSignRecordList() {
        List<SignRecord> signRecords = userService.selectSignRecordList();
        return BaseResult.ok(signRecords);
    }

    /**
     * showdoc
     *
     * @param identityCard  必选 string 身份证
     * @param communityId   必选 string 社区id
     * @param communityName 必选 string 社区名
     * @param houseNumber   必选 string 门牌号
     * @param username      必选 string 用户名
     * @param headPhotoUrl  可选 string 头像链接
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158502014539731","phone":"15123058497","username":"yanlong","integral":100,"isNameAuthentication":0,"identityCard":"513030199201088419","isHouseHead":1,"headPhotoUrl":"http://127.0.0.1:8052/upload/158502014539731.png","lastSignTime":1585031602000,"createTime":1585048945000,"communityId":1,"todayIntegralMax":100,"communityName":"聚金万佳苑","houseNumber":"10栋32-6"}}
     * @catalog 社区/用户
     * @title 完善个人信息
     * @description 完善个人信息
     * @url /cms/user/update
     */
    @RequestMapping("/update")
    @ValidateGroup(fileds = {
            //@ValidateFiled(index = 0,filedName = "phone",regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$")
            @ValidateFiled(index = 0, filedName = "identityCard", regStr = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)"),
            //@ValidateFiled(index = 0, filedName = "communityId", notNull = true),
            //@ValidateFiled(index = 0, filedName = "communityName", notNull = true),
            //@ValidateFiled(index = 0, filedName = "houseNumber", notNull = true),
            @ValidateFiled(index = 0, filedName = "username", notNull = true),
    })
    public BaseResult update(User user) {
        return userService.update(user);
        //return i > 0 ? BaseResult.ok(user) : BaseResult.build(StatusCode.USER_UPDATE_FAIL);
    }

    /**
     * showdoc
     *
     * @param headPhotoUrl 必选 string 用户头像链接
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 更改头像链接
     * @description 更改头像链接
     * @url /cms/user/updateHeadPicture
     */
    @RequestMapping("/updateHeadPicture")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
    })
    public BaseResult updateHeadPicture(String headPhotoUrl) {
        userService.updateHeadPicture(headPhotoUrl);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @param id 必选 string 用户id
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 确认户主
     * @description 确认户主
     * @url /cms/user/confirmHouseHead
     */
    @RequestMapping("/confirmHouseHead")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
    })
    public BaseResult confirmHouseHead(String id) {
        userService.confirmHouseHead(id);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @param phone    必选 string 电话
     * @param password 必选 string 密码
     * @param code     必选 string 验证码
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/用户
     * @title 用户修改电话号码
     * @description 用户修改电话号码
     * @url /cms/user/updatePhone
     */
    @RequestMapping("/updatePhone")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "password", notNull = true),
            @ValidateFiled(index = 0, filedName = "code", notNull = true),
    })
    public BaseResult updatePhone(UserVO userVO) {
        return userService.updatePhone(userVO);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158502014539731","phone":"15123058497","username":"yanlong","integral":100,"isNameAuthentication":0,"identityCard":"513030199201088419","isHouseHead":1,"headPhotoUrl":"http://127.0.0.1:8052/upload/158502014539731.png","lastSignTime":1585031602000,"createTime":1585048945000,"communityId":1,"todayIntegralMax":100,"communityName":"聚金万佳苑","houseNumber":"10栋32-6"}}
     * @catalog 社区/用户
     * @title 获取当前登录用户信息
     * @description 获取当前登录用户信息
     * @url /cms/user/selectCurrUser
     */
    @RequestMapping("/selectCurrUser")
    public BaseResult selectCurrUser() {
        User user = userService.selectCurrUserByPrimaryKey();
        return BaseResult.ok(user);
    }

    /****************************服务内部调用 feign*********************************/

    //查询用户信息
    @RequestMapping("/selectUserByKey")
    public BaseResult selectUserByKey(String userId) {
        User user = userService.selectByPrimaryKey(userId);
        return BaseResult.ok(user);
    }


    /**
     * 更新积分
     *
     * @param integral 增加或消耗的积分
     * @param relateId 相关id ，例如订单id
     * @param category 积分种类
     * @return
     */
    @RequestMapping("/updateIntegral")
    public BaseResult updateIntegral(Integer integral, String relateId, String category, String userId) {
        return userService.updateIntegral(integral, relateId, category, userId);
    }

    @RequestMapping("/selectUserCount")
    public BaseResult selectUserCount(String date) throws ParseException {
        return userService.selectUserCount(date);
    }

}
